package com.example.cheeseerpadmin.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cheeseerpadmin.sys.common.DataGridView;
import com.example.cheeseerpadmin.sys.common.ResultObj;
import com.example.cheeseerpadmin.sys.common.TreeNode;
import com.example.cheeseerpadmin.sys.entity.SysDept;
import com.example.cheeseerpadmin.sys.service.SysDeptService;
import com.example.cheeseerpadmin.sys.vo.DeptVo;
import org.apache.shiro.authc.AuthenticationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    public SysDeptService sysDeptService;

    @RequestMapping("/loadDeptLeftTreeJson")
    public DataGridView loadDeptLeftTreeJson() {
        List<SysDept> list = sysDeptService.list();
        ArrayList<TreeNode> treeNodes = new ArrayList<>();
        for (SysDept sysDept : list) {
            Boolean spread = sysDept.getOpen() == 1;
            treeNodes.add(new TreeNode(sysDept.getId(), sysDept.getPid(), sysDept.getTitle(), spread));
        }
        return new DataGridView(treeNodes);
    }

    @RequestMapping("/loadAllDept")
    public DataGridView loadAllDept(DeptVo deptVo) {
        Page<SysDept> page = new Page<>(deptVo.getPage(), deptVo.getLimit());
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getTitle()), "title", deptVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getAddress()), "address", deptVo.getAddress());
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getRemark()), "remark", deptVo.getRemark());
        queryWrapper.eq(deptVo.getId() != null, "id", deptVo.getId()).or();
        queryWrapper.eq(deptVo.getId() != null, "pid", deptVo.getId());
        queryWrapper.orderByAsc("ordernum");
        Page<SysDept> sysDeptPage = sysDeptService.page(page, queryWrapper);
        return new DataGridView(sysDeptPage.getTotal(), sysDeptPage.getRecords());
    }

    /**
     * 初始化排序码
     */
    @RequestMapping("/loadDeptMaxOrderNum")
    public Map<String, Object> loadDeptMaxOrderNum() {
        Map<String, Object> map = new HashMap<String, Object>();
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("ordernum");
        queryWrapper.orderByDesc("ordernum");
        List<SysDept> list = sysDeptService.list(queryWrapper);
        if (list.isEmpty()) {
            map.put("value", 1);
        } else {
            map.put("value", list.get(0).getOrdernum() + 1);
        }
        return map;
    }

    /**
     * 添加部门
     */
    @RequestMapping("/addDept")
    public ResultObj addDept(DeptVo deptVo) {
        //唐祺源在次一游，永远滴神
        try {
            deptVo.setCreatetime(new Date());
            sysDeptService.save(deptVo);
            return ResultObj.ADD_SUCCESS;
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 删除部门
     */
    @RequestMapping("/deleteDeptById")
    public ResultObj deleteDeptById(Integer id) {
        try {
            sysDeptService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 修改部门
     */
    @RequestMapping("/updateDept")
    public ResultObj updateDept(DeptVo deptVo) {
        try {
            sysDeptService.updateById(deptVo);
            return ResultObj.UPDATE_SUCCESS;
        } catch (AuthenticationException e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 查询是否有子节点
     */
    @RequestMapping("/checkDeptHasChildNode")
    public Map<String, Object> checkDeptHasChildNode(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", id);
        List<SysDept> list = sysDeptService.list(queryWrapper);
        if (list.size() > 0) {
            map.put("value", true);
        } else {
            map.put("value", false);
        }
        return map;
    }

}
